library(lavaan)
##Prepare data with sufficient statisitics##
mymeans<-matrix(c(3.06893, 2.92590, 3.11013), ncol=3,nrow=1)
mysd<-c(0.84194,0.88934,0.83470)
mat <- c(1.00000,
0.55226, 1.00000,
0.56256, 0.66307, 1.00000)
mycor <- getCov(mat, lower = TRUE)
##Transform correlation matrix to covariance matrix using information above##
myvarcov <- outer(mysd, mysd, FUN="*")
mycov <- mycor * myvarcov
rownames(mycor) <-c( "Glad", "Cheerful", "Happy")
colnames(mycor) <-c( "Glad", "Cheerful", "Happy")
rownames(mycov) <-c( "Glad", "Cheerful", "Happy")
colnames(mycov) <-c( "Glad", "Cheerful", "Happy")
mynob<-823
cr <- ztable(mycor, zebra = 2)
tab <- data.frame(Mean = round(mymeans[1, ], 3), SD = round(mysd, 3),
Var = round(mysd*mysd, 3))
pt <- t(cbind(mycor, tab))
ztable(pt, zebra = 2, caption = "Descriptive Statistics")
| Glad | Cheerful | Happy | |
|---|---|---|---|
| Glad | 1.00 | 0.55 | 0.56 |
\[ \Sigma = \Lambda \Psi \Lambda + \Theta \tag{1} \]
# Mplus file
l.cheer.inp
grViz("
digraph Cheer {
node [shape = circle]
Positive;
node [shape = box]
Cheer;
# Edges
Positive -> Cheer [label = <λ>];
Positive:n -> Positive:n [dir=both, label = <ψ>,position = N]
Cheer:s -> Cheer:s [dir=both, label = <θ>]
}
")
using correlations only (instead of variance/covariance matirx)
mod1<-'Positive =~ 1*Cheerful
Positive~~Positive
Cheerful~~0*Cheerful'
#Save output to fit1##
fit1<-cfa(mod1, sample.cov=mycov, sample.nobs = mynob, sample.mean=mymeans,
std.lv=F)
## Found more than one class "Model" in cache; using the first, from namespace 'MatrixModels'
##Request for summary of output##
summary(fit1, fit.measures=T)
## lavaan (0.5-20) converged normally after 9 iterations
##
## Number of observations 823
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
##
## Model test baseline model:
##
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
## P-value NA
##
## User model versus baseline model:
##
## Comparative Fit Index (CFI) 1.000
## Tucker-Lewis Index (TLI) 1.000
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -1070.768
## Loglikelihood unrestricted model (H1) -1070.768
##
## Number of free parameters 1
## Akaike (AIC) 2143.536
## Bayesian (BIC) 2148.249
## Sample-size adjusted Bayesian (BIC) 2145.074
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.000
## 90 Percent Confidence Interval 0.000 0.000
## P-value RMSEA <= 0.05 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.000
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
## Latent Variables:
## Estimate Std.Err Z-value P(>|z|)
## Positive =~
## Cheerful 1.000
##
## Variances:
## Estimate Std.Err Z-value P(>|z|)
## Positive 0.790 0.039 20.285 0.000
## Cheerful 0.000
grViz("
digraph CFA {
node [shape = circle]
Positive;
node [shape = box]
Glad; Cheer; Happy;
# Edges
Positive -> Glad [label = <λ<sub>1</sub>>];
Positive -> Cheer [label = <λ<sub>2</sub>>];
Positive -> Happy [label = <λ<sub>3</sub>>];
Positive:n -> Positive:n [dir=both, label = <ψ>]
Glad:s -> Glad:s [dir=both, label = <θ<sub>1</sub>>]
Cheer:s -> Cheer:s [dir=both, label = <θ<sub>2</sub>>]
Happy:s -> Happy:s [dir=both, label = <θ<sub>3</sub>>]
{rank = same; Positive;}
{rank = same; Glad; Cheer; Happy;}
}
")